import setupActions from 'actions/setup'
import { saveData } from 'utils'
import { message } from 'antd'

export default {
  state: {
    staffDetail: ''
  },
  reducers: {
    save (state, payload) {
      return saveData(state, payload)
    }
  },
  effects: dispatch => ({
    // 删除员工
    async delStaff (params) {
      this.save({
        delRequest: false
      })
      const res = await setupActions.delStaff(params)
      if (res.success) {
        this.save({
          delRequest: true
        })
      } else {
        message.error(res.msg)
      }
    },
    // 修改员工禁用状态
    async editStaffStatus (params) {
      this.save({
        editStaffStatusRequest: false
      })
      const res = await setupActions.editStaffStatus(params)
      if (res.success) {
        this.save({
          editStaffStatusRequest: true
        })
      } else {
        message.error(res.msg)
      }
    },
    // 修改密码
    async editStaffPassword (params) {
      this.save({
        editStaffPasswordRequest: false
      })
      const res = await setupActions.editStaffPassword(params)
      if (res.success) {
        this.save({
          editStaffPasswordRequest: true
        })
      } else {
        message.error(res.msg)
      }
    },
    // 修改员工基本信息
    async editStaffInfo (params) {
      this.save({
        staffInfo: false
      })
      const res = await setupActions.editStaffInfo(params)
      if (res.success) {
        this.save({
          staffInfo: true
        })
      } else {
        message.error(res.msg)
      }
    },
    // 修改用户信息
    async editUserInfo (params) {
      const res = await setupActions.editStaffInfo(params)
      if (res.success) {
        message.success('完成操作')
        if (params.center) {
          localStorage.setItem('nickname', params.name)
          localStorage.setItem('avatar', params.avatar)
        }
      } else {
        message.error(res.msg)
      }
    },
    // 查询员工详情
    async getStaffDetail (params) {
      // this.save({
      //   staffDetail: ''
      // })
      const res = await setupActions.getStaffDetail(params)
      if (res.success) {
        this.save({
          staffDetail: res.data
        })

        return res
      } else {
        message.error(res.msg)
      }
    },
    // 查询员工列表
    async getStaffList (params) {
      this.save({
        staffData: null
      })
      const res = await setupActions.getStaffList(params)
      if (res.success) {
        this.save({
          staffData: {
            list: res.data && res.data.userResList,
            total: res.data && res.data.totalCount
          }
        })
      } else {
        message.error(res.msg)
      }
    },
    // 创建员工账号
    async createStaffAccount (params) {
      this.save({
        createRequest: false
      })
      const res = await setupActions.createStaffAccount(params)
      if (res.success) {
        this.save({
          createRequest: true
        })
      } else {
        message.error(res.msg)
      }
    },
    // 获取部门列表（参数）
    async getDepartmentList (params) {
      const res = await setupActions.getDepartmentList(params)
      const departmentList = []
      res.data.userResList && res.data.userResList.length > 0 && res.data.userResList.map(item => {
        departmentList.push({
          text: item.deptName,
          value: item.deptId
        })
      })
      if (res.success) {
        this.save({
          departmentList
        })
      } else {
        message.error(res.msg)
      }
    },
    // 获取部门列表
    async getDepartment (params) {
      this.save({
        department: null
      })
      const res = await setupActions.getDepartment(params)
      if (res.success) {
        this.save({
          department: {
            total: res.data.totalCount,
            list: res.data.userResList
          }
        })
      } else {
        message.error(res.msg)
      }
    },
    // 新增部门
    async createDep (params) {
      try {
        await setupActions.createDep(params)
      } catch (error) {

      }
    },
    // 编辑部门
    async editDept (params) {
      try {
        await setupActions.editDept(params)
      } catch (error) {

      }
    },
    // 删除部门
    async delDepartment (params) {
      this.save({
        delDepartmentRequest: false
      })
      const res = await setupActions.delDepartment(params)
      if (res.success) {
        this.save({
          delDepartmentRequest: true
        })
      } else {
        message.error(res.msg)
      }
    },
    // 修改禁用状态
    async updateDepStatus (params) {
      this.save({
        updateDepStatusRequest: false
      })
      const res = await setupActions.updateDepStatus(params)
      if (res.success) {
        this.save({
          updateDepStatusRequest: true
        })
      } else {
        message.error(res.msg)
      }
    },
    // 获取登陆日志
    async getLoginList (params) {
      this.save({
        loginData: null
      })
      const res = await setupActions.getLoginList(params)
      if (res.success) {
        this.save({
          loginData: {
            total: res.data.totalCount,
            list: res.data.loginRecordList
          }
        })
      } else {
        message.error(res.msg)
      }
    },
    // 获取操作日志
    async getOperateList (params) {
      this.save({
        operateData: null
      })
      const res = await setupActions.getOperateList(params)
      if (res.success) {
        this.save({
          operateData: {
            total: res.data.totalCount,
            list: res.data.operateLogResList
          }
        })
      } else {
        message.error(res.msg)
      }
    },
    // 获取角色列表-分页
    async getRoles (params) {
      try {
        this.save({
          rolesData: {}
        })
        const res = await setupActions.getRoles(params)
        if (res.success) {
          this.save({
            rolesData: {
              total: res.data.totalCount || 0,
              list: res.data.roleResList || []
            }
          })
        }
      } catch (error) {

      }
    },
    // 删除角色
    async delRole (params) {
      await setupActions.delRole(params)
    },
    // 编辑角色
    async editRole (params) {
      await setupActions.editRole(params)
    },
    // 新增角色
    async addRole (params) {
      await setupActions.addRole(params)
    },
    // 获取角色列表
    async getRoleListOptions (params) {
      const res = await setupActions.getRoleListOptions(params)
      if (res.success) {
        let roleListOptions = []
        res.data.map(item => {
          roleListOptions.push({
            text: item.name,
            value: item.roleId
          })
        })
        this.save({
          roleOptions: roleListOptions
        })
      }
    },
    // 获取权限树
    async getAuthTree (params) {
      this.save({
        authTree: []
      })
      const res = await setupActions.getAuthTree(params)
      if (res.success) {
        this.save({
          authTree: res.data
        })
      }
    },
    async getAuth (params) {
      this.save({
        roleAuth: []
      })
      const res = await setupActions.getAuth(params)
      if (res.success) {
        this.save({
          roleAuth: res.data
        })
      }
    },

    // 设置权限
    async setAuth (params) {
      await setupActions.setAuth(params)
    },
    // 获取协议列表
    async getProtocolList (params) {
      this.save({
        protocolData: {}
      })
      const res = await setupActions.getProtocolData(params)
      if (res.success) {
        this.save({
          protocolData: res.data
        })
      }
    },
    // 获取协议详情
    async getProtocolDetail (params) {
      this.save({
        protocolDetail: {}
      })
      const res = await setupActions.getProtocolDetail(params)
      if (res.success) {
        this.save({
          protocolDetail: res.data
        })
      }
    },
    // 编辑、新增协议
    async addOrUpdateProtocol (params) {
      await setupActions.addOrUpdateProtocol(params)
    },
    // 修改协议状态
    async updateProtocolStatus (params) {
      await setupActions.updateProtocolStatus(params)
    },
    // 删除协议
    async delProtocol (params) {
      await setupActions.delProtocol(params)
    },
    // 获取帮助列表
    async getHelpList (params) {
      this.save({
        helpData: {}
      })
      const res = await setupActions.getHelpList(params)
      if (res.success) {
        this.save({
          helpData: res.data
        })
      }
    },
    // 获取帮助详情
    async getHelpDetail (params) {
      this.save({
        helpDetail: {}
      })
      const res = await setupActions.getHelpDetail(params)
      if (res.success) {
        this.save({
          helpDetail: res.data
        })
      }
    },
    // 编辑、新增帮助
    async addOrUpdateHelp (params) {
      await setupActions.addOrUpdateHelp(params)
    },
    // 修改帮助状态
    async updateHelpStatus (params) {
      await setupActions.updateHelpStatus(params)
    },
    // 删除帮助
    async delHelp (params) {
      await setupActions.delHelp(params)
    },
    async addOrUpdateAuth (params) {
      await setupActions.addOrUpdateAuth(params)
    }
  })
}
